package org.cancan.demo.panel;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class CameraFrame extends JFrame {
    private CustomPanel customPanel;
    private BufferedImage currentImage;

    public CameraFrame() {
        setTitle("USB Camera with OpenCV");
        setSize(640, 480);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        customPanel = new CustomPanel();
        add(customPanel, BorderLayout.CENTER);

        setVisible(true);
    }

    public void setImage(BufferedImage image) {
        if (image != null) {
            this.currentImage = image;
            SwingUtilities.invokeLater(() -> {
                customPanel.repaint(); // 重绘面板
            });
        }
    }

    public class CustomPanel extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            // 绘制当前图像
            if (currentImage != null) {
                g.drawImage(currentImage, 0, 0, this);
            }

            // 在图像上绘制图形
            g.setColor(Color.RED);
            g.fillRect(50, 50, 100, 50); // 画一个红色矩形
            g.setColor(Color.BLUE);
            g.drawOval(200, 200, 100, 100); // 画一个蓝色圆圈
            g.setColor(Color.GREEN);
            g.drawLine(300, 300, 400, 400); // 画一条绿色线
        }
    }
}